package com.hexus.eve.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hexus.eve.entities.Ore;
//import com.hexus.eve.entities.Refinables;
import com.hexus.eve.utils.HibernateUtil;
import com.opensymphony.xwork2.ActionSupport;

public class OreMineralsTable extends ActionSupport {
	@Autowired
	private SessionFactory sessionFactory;
	
	private static final long serialVersionUID = 1L;
	
	private List<Ore> oreMineralTable;
	private List<Ore> compressedOreMineralTable;
	
	@SuppressWarnings("rawtypes")
	private Map mineralsMap = new TreeMap();
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String execute() {
		
		String result = SUCCESS;
		//Session session = HibernateUtil.getSessionFactory().openSession();
		Session session = sessionFactory.openSession();
		List<Object[]> items = session.createSQLQuery("SELECT * FROM Minerals")
			.addScalar("id", Hibernate.INTEGER)
			.addScalar("name", Hibernate.STRING).list();
				
		for (Object[] item: items){
			mineralsMap.put(item[0], item[1]);
		}
		
		items = session.createSQLQuery("SELECT * FROM Ores WHERE name not like 'Compressed %'")
			.addScalar("id", Hibernate.INTEGER)
			.addScalar("name", Hibernate.STRING).list();
		Map ores = new TreeMap();
		
		for (Object[] item: items) {
			ores.put(item[0], item[1]);
		}
		
		items = session.createSQLQuery("SELECT * FROM Ores WHERE name like 'Compressed %'")
			.addScalar("id", Hibernate.INTEGER)
			.addScalar("name", Hibernate.STRING).list();
		Map compressedOres = new TreeMap();
	
		for (Object[] item: items) {
		compressedOres.put(item[0], item[1]);
		}
		
		items = session.createSQLQuery("SELECT OreMinerals.marketGroupID, oreID, mineralID, quantity, volume, portionSize FROM OreMinerals JOIN invTypes ON OreMinerals.oreID = invTypes.typeID WHERE oreName not like 'Compressed %' ORDER BY marketGroupID, oreID")
			.addScalar("marketGroupID", Hibernate.INTEGER)
			.addScalar("oreID", Hibernate.INTEGER)
			.addScalar("mineralID", Hibernate.INTEGER)
			.addScalar("quantity", Hibernate.INTEGER)
			.addScalar("volume", Hibernate.FLOAT)
			.addScalar("portionSize", Hibernate.INTEGER).list();
		
		List<Object[]> compressedItems = session.createSQLQuery("SELECT OreMinerals.marketGroupID, oreID, mineralID, quantity, volume, portionSize FROM OreMinerals JOIN invTypes ON OreMinerals.oreID = invTypes.typeID WHERE oreName like 'Compressed %' ORDER BY marketGroupID, oreID")
			.addScalar("marketGroupID", Hibernate.INTEGER)
			.addScalar("oreID", Hibernate.INTEGER)
			.addScalar("mineralID", Hibernate.INTEGER)
			.addScalar("quantity", Hibernate.INTEGER)
			.addScalar("volume", Hibernate.FLOAT)
			.addScalar("portionSize", Hibernate.INTEGER).list();
		
		/*List<Object[]> refinablesItems = session.createSQLQuery("SELECT OreMinerals.marketGroupID, oreID, mineralID, quantity, volume, portionSize FROM OreMinerals JOIN invTypes ON OreMinerals.oreID = invTypes.typeID WHERE oreName like 'Compressed %' ORDER BY marketGroupID, oreID")
			.addScalar("marketGroupID", Hibernate.INTEGER)
			.addScalar("oreID", Hibernate.INTEGER)
			.addScalar("mineralID", Hibernate.INTEGER)
			.addScalar("quantity", Hibernate.INTEGER)
			.addScalar("volume", Hibernate.FLOAT)
			.addScalar("portionSize", Hibernate.INTEGER).list();*/
		
		session.close();
		
		oreMineralTable = new ArrayList<Ore>(items.size());
		Ore ore = new Ore((Integer)items.get(0)[1], (String)ores.get((Integer)items.get(0)[1]), (Integer)items.get(0)[0], (Float)items.get(0)[4], (Integer)items.get(0)[5]);
		Integer previousId = ore.getId();
		for (Object[] item: items) {
			Integer id = (Integer) item[1];
			if (previousId.intValue() != id.intValue()) {
				oreMineralTable.add(ore);
				ore = new Ore(id, (String)ores.get(id), (Integer)item[0], (Float)item[4], (Integer)item[5]);
				previousId = id;
			}
			ore.getMinerals().put((Integer)item[2], (Integer)item[3]);
		}
		oreMineralTable.add(ore);
		
		compressedOreMineralTable = new ArrayList<Ore>(compressedItems.size());
		Ore compressedOre = new Ore((Integer)compressedItems.get(0)[1], (String)compressedOres.get((Integer)compressedItems.get(0)[1]), (Integer)compressedItems.get(0)[0], (Float)compressedItems.get(0)[4], (Integer)compressedItems.get(0)[5]);
		previousId = compressedOre.getId();
		for (Object[] item: compressedItems) {
			Integer id = (Integer) item[1];
			if (previousId.intValue() != id.intValue()) {
				compressedOreMineralTable.add(compressedOre);
				compressedOre = new Ore(id, (String)compressedOres.get(id), (Integer)item[0], (Float)item[4], (Integer)item[5]);
				previousId = id;
			}
			compressedOre.getMinerals().put((Integer)item[2], (Integer)item[3]);
		}
		compressedOreMineralTable.add(compressedOre);
			
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public Map getMineralsMap() {
		return mineralsMap;
	}

	public List<Ore> getOreMineralTable() {
		return oreMineralTable;
	}
	
	public List<Ore> getCompressedOreMineralTable() {
		return compressedOreMineralTable;
	}
}
 